package pro.softzhang.algo.lc0;

import com.alibaba.fastjson.JSON;
import pro.softzhang.algo.flag.theme.TAnagram;

import java.util.*;

/**
 *
 */
@TAnagram
public class LC49_GroupAnagrams {
    public static void main(String[] args) {
        String[] strs = {"eat", "tea", "tan", "ate", "nat", "bat"};
        List<List<String>> result = new Solution().groupAnagrams(strs);
        System.out.println(JSON.toJSONString(result));
    }

    static
    class Solution {
        public List<List<String>> groupAnagrams(String[] strs) {
            Map<String, List<String>> groups = new HashMap<>();
            for (String str : strs) {
                char[] arr = str.toCharArray();
                Arrays.sort(arr);
                String sorted = new String(arr);
                groups.putIfAbsent(sorted, new ArrayList<>());
                groups.get(sorted).add(str);
            }
            return new ArrayList<>(groups.values());
        }
    }
}